package com.klun.likou.base;

public class ListNode {
	public int val;
	public ListNode next;

	public boolean hasNext() {
		return null != next;
	}

	public ListNode next() {
		return next;
	}

	public ListNode(int val) {
		this.val = val;
	}

	// 升序
	public static ListNode genAscNode(int size) {
		ListNode head = new ListNode(1);
		ListNode result = head;
		for (int i = 2; i < size; i++) {
			head.nextNode(i);
			head = head.next;
		}
		return result;
	}

	public ListNode() {
	}

	public ListNode next(ListNode a) {
		return next = a;
	}

	public ListNode nextNode(int val) {
		return this.next = new ListNode(val);
	}

	public void setNext(ListNode next) {
		this.next = next;
	}

	public int getVal() {
		return val;
	}

	public void setVal(int val) {
		this.val = val;
	}

	public void print() {
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		sb.append(this.val + ",");
		ListNode curr = this;
		// 双指针判断是否有死循环
		ListNode a = this;
		ListNode b = this.next;
		while (true) {
			if (null == b) {
				break;
			}
			if (a == b) {
				System.out.println("链表上有环");
				return;
			}
			a = a.next;
			if (b.next != null) {
				b = b.next.next;
			} else {
				break;
			}
		}

		while (curr.hasNext()) {
			sb.append(curr.next().val + ",");
			curr = curr.next;
		}
		sb.append("]");
		System.out.println(sb.toString());
	}
}
